package cs236369.webserver.queue.handlers;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import java.util.concurrent.ConcurrentLinkedQueue;

import org.apache.http.impl.DefaultHttpServerConnection;
import org.apache.http.params.HttpParams;
import org.apache.http.protocol.BasicHttpContext;


import cs236369.webserver.DTOs.Request;
import cs236369.webserver.queue.managers.RequestTaskManager;

public class SocketHandler extends Worker<Socket> {

	private HttpParams params = null;
	private RequestTaskManager rtm = null;

	public SocketHandler(ConcurrentLinkedQueue<Socket> queue , RequestTaskManager rtm, HttpParams params) {
		super(queue);
		this.rtm = rtm;
		this.params = params;
	}

	@Override
	protected void handleTask(Socket socket) {
		// Read the request parse it then put the parsed request object in the
		// queue
		try {
			//BufferedReader reader = new BufferedReader(new InputStreamReader(
			//		socket.getInputStream()));
			//String firstLine = reader.readLine();
			//if (firstLine == null) {
				// return null;
			
			//}
			DefaultHttpServerConnection conn = new DefaultHttpServerConnection();
			conn.bind(socket, this.params);
            Request r = new Request(conn,new BasicHttpContext(null));
            rtm.enQueue(r);
			System.out.println("Check point");
		} catch (IOException e) {

			e.printStackTrace();
		}
	}

}
